<?php
namespace app\exam\model;

use app\exam\model\CandidateModel;
class ExamModel extends CommonModel
{
    /*
        获取当前考试列表
        return [
            [
                'exam_id' => '1', //考试科目id
                'exam_title' => '测试考试1', //考试科目名称
            ],
            [
                'exam_id' => '2', //考试科目id
                'exam_title' => '测试考试2', //考试科目名称
            ]
        ]
    */
    public function getExamList()
    {
        $Dao = db();
        $time = time();
        $sql = 'select exam_id,exam_title from exam where exam_end_time > ? and exam_start_time < ? and exam_status = 1 order by exam_id desc';
        $params = array(
            $time,
            $time
        );

        $Exam = $Dao
            ->query($sql, $params);

        return empty($Exam) == false ? $Exam : array(); 
    }

    /*
        获取考试详情
        params:
            exam_id
        return:
            array
            boolean
    */
    public function getExamInfo($exam_id = 0)
    {
        $Dao = db();
        $sql = 'select exam_title,exam_expire_time,exam_subject_cnt from exam where exam_id = ?';
        $params = array(
            $exam_id
        );

        $examInfo = $Dao
            ->query($sql, $params);

        return empty($examInfo) == false ? $examInfo[0] : false;
    }

    /*
        检查当前考试可用状态
        params:
            exam_id 考试id
        return:
            boolean 
    */
    public function checkExamStatus($exam_id = 0)
    {
        $examList = $this->getExamList();

        foreach ($examList as $value) {
            if($value['exam_id'] == $exam_id){
                return true;
            }
        }
        return false;
    }

    /*
        检查考生考试状态
        params:
            candidate_id
        return:
            boolean
    */
    public function checkCandidateExamStatus($token = 0)
    {
        $Dao = db();
        $CandidateDao = $Dao->table('candidate');

        $sql = 'select candidate_status from candidate where candidate_token = ? limit 1';
        $params = array(
            $token
        );
        $Candidate = $$CandidateDao
            ->query($sql, $params);

        return $Candidate[0]['candidate_status'] == '1' ? true : false;
    }

    /*
        获取考试考试信息
        params:
            token 用户token
        return:
            array
    */
    public function getCandidateExamInfo($token = '')
    {
        $CandidateModel = new CandidateModel();
        $candidateInfo = $CandidateModel->getCandidateInfoByToken($token);
        $examInfo = $this->getExamInfo($candidateInfo['fk_exam_id']);

        if(empty($candidateInfo) || empty($examInfo)){
            return false;
        }

        $end_time = strtotime($candidateInfo['candidate_createtime']) + $examInfo['exam_expire_time'];
        $expire_time = $end_time - time();
        $expire_time = $expire_time > 0 && $expire_time < $examInfo['exam_expire_time'] ? $expire_time : -1;

        $resExam = array(
            'id' => $candidateInfo['fk_exam_id'],
            'title' => $examInfo['exam_title'],
            'expire_time' => $expire_time,
            'subject_cnt' => $examInfo['exam_subject_cnt']
        );
        $resCandidate = array(
            'login_name' => $candidateInfo['candidate_login_name'],
            'token' =>  $token
        );

        $res = array(
            'candidate' => $resCandidate,
            'exam' => $resExam
        );
        return $res;
    }
}
?>